import com.atguigu.bigdata.gmall.realtime.app.BaseSQLApp;
import com.atguigu.bigdata.gmall.realtime.bean.WaterSensor;
import org.apache.flink.api.common.eventtime.WatermarkStrategy;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.table.api.Table;
import org.apache.flink.table.api.bridge.java.StreamTableEnvironment;

import static org.apache.flink.table.api.Expressions.$;

/**
 * @Author lzc
 * @Date 2022/10/14 10:44
 */
public class TvfDemo1 extends BaseSQLApp {
    public static void main(String[] args) {
        new TvfDemo1().init(
            20000,
            1,
            "TvfDemo1"
        );
    }
    
    @Override
    protected void handle(StreamExecutionEnvironment env,
                          StreamTableEnvironment tEnv) {
        SingleOutputStreamOperator<WaterSensor> stream = env
            .fromElements(
                new WaterSensor("s1", 1000L, 10),
                new WaterSensor("s1", 2000L, 20),
                new WaterSensor("s1", 4000L, 30),
                new WaterSensor("s1", 6000L, 40),
                new WaterSensor("s1", 8000L, 50),
                new WaterSensor("s2", 6000L, 30)
            )
            .assignTimestampsAndWatermarks(
                WatermarkStrategy
                    .<WaterSensor>forMonotonousTimestamps()
                    .withTimestampAssigner((ws, ts) -> ws.getTs())
            );
        
        
        Table table = tEnv.fromDataStream(stream, $("id"), $("ts").rowtime(), $("vc"));
        // tvf 滚动
        tEnv.createTemporaryView("sensor", table);
       /* tEnv.sqlQuery("select " +
                          " window_start, " +
                          "window_end, " +
                          "id," +
                          "sum(vc) vc_sum " +
                          "from table( tumble( table sensor, descriptor(ts), interval '5' second) ) " +
                          "group by window_start, window_end, id ")
            .execute()
            .print();*/
       /* tEnv.sqlQuery("select " +
                          " window_start, " +
                          "window_end, " +
                          "id," +
                          "sum(vc) vc_sum " +
                          "from table( tumble(" +
                          "         DATA=> table sensor, " +
                          "         TIMECOL => descriptor(ts), " +
                          "         SIZE => interval '5' second) ) " +
                          "group by window_start, window_end, id ")
            .execute()
            .print();*/
        // 滑动
        /*tEnv.sqlQuery("select " +
                          " hop_start(ts, interval '2' second, interval '5' second), " +
                          " hop_end(ts, interval '2' second, interval '5' second), " +
                          " id, " +
                          " sum(vc) vc_sum " +
                          "from sensor " +
                          "group by id, hop(ts, interval '2' second, interval '5' second)")
            .execute()
            .print();*/
        
        
       /* tEnv.sqlQuery("select " +
                          " window_start, " +
                          "window_end, " +
                          "id," +
                          "sum(vc) vc_sum " +
                          "from table( hop( table sensor, descriptor(ts), interval '2' second, interval '6' second) ) " +
                          "group by window_start, window_end, id ")
            .execute()
            .print();*/
        
        
    }
}
